import config from '../common/config'
let appID = config.app_id
export default (options) => {
	let token = uni.getStorageSync('token') || '';
	let userid = uni.getStorageSync('userid')
	let headertoken = token ? {
		'Authorization': token
	} : {};
	const {
		url,
		method = 'GET',
		data,
		header,
		flag
	} = options;
	uni.showLoading({
		title: '加载中'
	})
	let jsonUser = {}
	if (userid) {
		jsonUser.userid = userid
	}
	return new Promise((resolve, reject) => {
		uni.request({
			data: {
				app_id: appID,
				...jsonUser,
				...data
			},
			method,
			url: flag ? url : url + '?userid=' + userid,
			// url,
			header: flag ? {} : { ...header,
				...headertoken
			},
			success(res) {
				// if (res.data.code === 401) { // 401 token失效
				// 	uni.redirectTo({
				// 		url: '/pages/login/login'
				// 	})
				// }
				if (res.statusCode === 200) {
					if (res.data.code !== '1')
						// uni.showToast({
						// 	icon: 'none',
						// 	title: res.data.msg || ''
						// })
						resolve(res.data)
				} else {
					reject(res)
				}
				uni.hideLoading()
			},
			fail: (err) => {
				console.log(err)
			}
		})
	})
}
